﻿using System;
using System.Collections.Generic;
using System.Text;
using SpeechLib;
using System.Reflection;

namespace UTIL
{
    public class CJapanVoice : IDisposable 
    {
        //TTS 使用
        private  SpVoice JapanVoice = null;       
        //判断日语朗读环境是否存在
        private bool bExist = false;
        private bool disposed = false;
        //TTS 播放标志
        SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFDefault;
        public CJapanVoice(CConfigXml cf)
        {
            try
            {
                if (cf.GetAppSettings("voiceJapaneseTypeTTS") == "1")
                {
                    JapanVoice = new SpVoice();
                    JapanVoice.Voice = JapanVoice.GetVoices("", "").Item(Convert.ToInt32(cf.GetAppSettings("voiceJapan")));
                    int iVolume = Convert.ToInt32(cf.GetAppSettings("volume"));
                    int iVoiceRate = Convert.ToInt32(cf.GetAppSettings("voiceRate"));
                    JapanVoice.Volume = iVolume;
                    JapanVoice.Rate = iVoiceRate;
                    bExist = true;
                }
                
            }
            catch (Exception me)
            {
                bExist = false;
                UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());
            }
         
        }

        public bool 能否使用()
        {
            return bExist;
        }
        public void SpeakJapan(String strWord)
        {
            if (bExist)
            {
                JapanVoice.Speak(strWord, SpFlags);
            }
        }

        #region 释放资源
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        private void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {

                    try
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(JapanVoice);
                        JapanVoice = null;                                            
                    }
                    catch (Exception)
                    {

                    }

                }
                disposed = true;
            }
        }
        #endregion       

    }
    public class CChineseVoice:IDisposable 
    {
        //TTS 使用
        private static SpVoice ChineseVoice = null;       
        //判断汉语朗读环境是否存在
        private bool bExist = false;
        //TTS 播放标志
        SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFDefault;
        private bool disposed = false;       
        public CChineseVoice(CConfigXml cf)
        {
            try
            {
                if (cf.GetAppSettings("voiceChineseTypeTTS") == "1")
                {
                    ChineseVoice = new SpVoice();
                    ChineseVoice.Voice = ChineseVoice.GetVoices("", "").Item(Convert.ToInt32(cf.GetAppSettings("voiceChinese")));
                    int iVolume = Convert.ToInt32(cf.GetAppSettings("volume"));
                    int iVoiceRate = Convert.ToInt32(cf.GetAppSettings("voiceChinese"));
                    ChineseVoice.Volume = iVolume;
                    ChineseVoice.Rate = iVoiceRate;
                    bExist = true;
                }
            }
            catch (Exception me)
            {
                bExist = false;
                UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, me, MethodBase.GetCurrentMethod().GetType());
            }
         
        }

        public bool 能否使用()
        {
            return bExist;
        }
        public void SpeakChinese(String strWord)
        {
            if (bExist)
            {
                ChineseVoice.Speak(strWord, SpFlags);
            }
        }

        #region 释放资源
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        private void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {

                    try
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(ChineseVoice);
                        ChineseVoice = null;                        
                    }
                    catch (Exception)
                    {

                    }

                }
                disposed = true;
            }
        }
        #endregion       

    }
}
